<!DOCTYPE html>
<html lang="en">
<head>
    {{template "macro60_head_script" .}}
</head>
<body>
{{template "macro60_header"}}

<div class="container" id="container" >
    <div class="row" id="content">
        <div class="col-sm-12" id="edit_macro">
            <form action="" method="post" class="col-sm-10" id="addMacroForm">
                <style>
                    .margin_bottom {
                        margin-bottom: 10px;
                    }
                </style>
                <div class="form-group">
                    <label for="profession">选择宏所属职业：</label>
                    <select class="form-control col-xs-12 col-sm-12 col-md-2 margin_bottom" id="profession" name="profession_id" v-model="select_proid" v-on:change="changeProfession">
                        <option value="">
                            请选择
                        </option>
                        <option v-for="item in professions" v-bind:value="item.id">
                        ${item.name}$
                        </option>
                    </select>
                </div>
                <div class="form-group">
                    <label for="title">标题：</label>
                    <input name="title" type="text" class="form-control" id="title" placeholder="请输入宏模板的一小段使用介绍">
                </div>
                <div class="form-group">
                    <label for="macro">宏模板：</label>
                    <textarea name="macro" class="form-control" id="macro" placeholder="请将要分享的宏模板粘贴在此处！
比如：
#showtooltip
/cast [nostance:1,nostance:3]激活
/cast [stance:1]野性冲锋(熊形态)
/castsequence [stance:3] reset=10 野性冲锋(猎豹形态),毁灭
/script UIErrorsFrame:Clear()" style="min-height: 300px;"></textarea>
                </div>
                <div class="form-group">
                    <label for="title">服务器-角色名称：</label>
                    <input name="author" type="text" class="form-control" id="author" placeholder="请输入分享者信息！格式：服务器-角色名称">
                </div>
                <button type="button" class="btn btn-info col-sm-12" v-on:click="submitToService">分享一条宏</button>
            </form>
        </div>
    </div>
    {{template "common_footer"}}
</div>
<br><br>
<script>
    var vue = new Vue({
        el: "#container",
        delimiters: ['${', '}$'],
        data: {
            professions: [
            ],
            select_proid: '',
            select_pro: [],
            select_mastery: [],
        },
        methods: {
            changeProfession: function() {
                var len = vue.$data.professions.length;
                // console.log(len);
                if (len <= 0) {
                    return false;
                }
                for (var i = 0; i < len; i++) {
                    if (vue.$data.select_proid === vue.$data.professions[i].id) {
                        vue.$data.select_pro = vue.$data.professions[i];
                        // this.select_mastery = this.select_pro.child;
                    }
                }
            },
            submitToService: function() {
                var pid = document.getElementById("profession").value;
                if (!pid) {
                    alert('职业不能为空！');
                    return false;
                }
                var title = document.getElementById("title").value;
                var macro = document.getElementById("macro").value;
                if (!title || !macro) {
                    alert('标题或宏模板不能为空！');
                    return false;
                }

                var author = document.getElementById("author").value;
                if (!author) {
                    alert('请留下你的艾泽拉斯名称，分享者必将被铭记！');
                    return false;
                }
                axios.post('/macro60/', {
                    professionId: parseInt(pid),
                    title: title,
                    author: author,
                    macro: macro,
                })
                        .then(function(response) {
                            document.getElementById("title").value = "";
                            document.getElementById("macro").value = "";
                            alert("分享成功，谢谢你的无私分享！");
                        });
            }
        },
        computed: {
            select_mastery: function() {
                if (vue.$data.select_pro) {
                    return vue.$data.select_pro.child;
                }
                return [];
            }
        },
    });
	axios.get('/macro/professionList', {
		params: {
			v: 60,
		}
	})
		.then(function(response) {
			vue.$data.professions = response.data.data;
		});
</script>
</body>
</html>